'''
    grad_app.py

        The 'grad_app.py' module contains just one class called
        'app' which is used by the gui to access the backend of
        the system
    
        app

            The 'app' class contains all the methods and information
            that is relevant to the GRAD9001 application such as:
            the student and filter instances and the current courses
'''

import student
import filter

'''
    app

    The 'app' class contains all the methods and information
    that is relevant to the GRAD9001 application such as:
    the student and filter instances and the current courses
'''
class app:

    '''
        __init__()

        Initializes the application with a student, a filter
        and the current courses
    '''
    def __init__(self, name, major, minor):
        self.student_user = student.student(name, major, minor)
        self.filter = filter.filter(self.student_user)
        self.current_courses = []
        self.years = 4
        self.save_name = None
        
    '''
        get_courses()

        Returns courses that matched the filter/search
    '''
    def get_courses(self, search_options, year):
        self.current_courses = self.filter.get_courses(search_options, year)
        return self.current_courses
    
    def get_student_course_info(self, course_id, year):
        for course in self.get_student().get_courses():
            if course.is_named(course_id):
               return self.line_break(course._course.get_description()), \
               self.student_user.get_valid_semesters(course._course, year)

    '''
        get_course_info()
        
        Returns all the information of a particular course
    '''
    def get_course_info(self, course_id, year):
        for course in self.current_courses:
            if course.is_named(course_id):
                return self.line_break(course.get_description()), \
                self.student_user.get_valid_semesters(course, year)
    
    '''
        add_course()

        Adds a course to the particular semester in the year
    '''
    def add_course(self, course_id, semester_num):
        for course in self.current_courses:
            if course.is_named(course_id):
                self.student_user.add(course, semester_num)
    
    '''
        get_semester_courses()

        Returns all the courses in a semester in a string
        array
    '''
    def get_semester_courses(self, semester):
        display_strings = []
        courses = self.student_user.courses_being_taken(semester)
        for course in courses:
            display_strings.append(course.display_string())
        return display_strings
    
    '''
        change_grade()

        Allows the student to change the course grade
    '''
    def change_grade(self, course, semester, grade):
        self.student_user.change_grade(course, semester, grade)
        
    '''
        can_remove_course()

        Checks if the course can safely be removed from
        the plan
    '''
    def can_remove_course(self, course, semester):
        return self.student_user.can_remove(course, semester)
    
    '''
        remove_course()

        Removes the course from the plan
    '''
    def remove_course(self, course, semester):
        self.student_user.remove_course(course, semester)
        
    '''
        line_break()

        Inserts line breaks into text after every 80 
        characters
    '''
    def line_break(self, text):
        new_text = ""
        current_line = 0
        text = text.split()
        
        for word in text:
            if current_line + len(word) > 80:
                new_text += '\n'
                current_line = 0
            else:
                new_text += ' '
            new_text += word
        return new_text
    
    '''
        get_student()

        Returns the student object
    '''
    def get_student(self):
        return self.student_user
            
